package com.chromatic.competition.utils;

import com.chromatic.competition.inteface.UserInitializationService;
import com.chromatic.competition.inteface.UserPostService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Retrofit 客户端工具类
 */
public class RetrofitClientUtil {
    private static final String BASE_URL = "https://api.fzuenactus.org.cn/api/";
    private static RetrofitClientUtil instance;
    private final Retrofit retrofit;

    private RetrofitClientUtil() {
        // 创建 HTTP 日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        // 创建 OkHttpClient
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();

        // 创建 Retrofit
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取 RetrofitClientUtil 实例
     *
     * @return RetrofitClientUtil 实例
     */
    public static synchronized RetrofitClientUtil getInstance() {
        if (instance == null) {
            instance = new RetrofitClientUtil();
        }
        return instance;
    }

    /**
     * 获取用户初始化服务的 API 接口
     *
     * @return UserInitializationService 实例
     */
    public UserInitializationService getApiService() {
        return retrofit.create(UserInitializationService.class);
    }

    /**
     * 获取用户发布服务的 API 接口
     *
     * @return UserPostService 实例
     */
    public UserPostService getPostService() {
        return retrofit.create(UserPostService.class);
    }
}